package ezonsport.android.service;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import android.util.Log;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.stmt.QueryBuilder;

import ezonsport.android.core.stepSystem.SportData;
import ezonsport.android.core.stepSystem.StepData;
import ezonsport.android.core.stepSystem.Watch;
import ezonsport.android.core.systemManage.User;
import ezonsport.android.data.DataHelper;
import ezonsport.android.data.OrmliteDataContext;

/**
 * @author Administrator chenbei
 * @since 2012-6-11
 * 
 */
public class SportDataService {
	OrmliteDataContext ctx;

	public SportDataService(DataHelper dataHelper) {
		ctx = new OrmliteDataContext(dataHelper);
	}

	/**
	 * 获取指定时间段的计步数据
	 * 
	 * @param from
	 * @param to
	 * @return List<StepData>
	 */
	public List<StepData> getStepData(Date from, Date to) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(to);
		endCalendar.add(Calendar.DATE, 1);
		String start = format.format(from);
		String end = format.format(endCalendar.getTime());
		try {
			QueryBuilder<StepData, Integer> qb = ctx
					.getQueryBuilder(StepData.class, int.class)
					.selectRaw("collectTime", "sum(steps)")
					.groupBy("collectTime");
			qb.where().raw(
					"collectTime between '" + start + "' and '" + end + "'");
			List<String[]> stepList = ctx.queryBySql(StepData.class, int.class,
					qb.prepareStatementString());
			System.out.println("size======="+stepList.size());
			List<StepData> list = new ArrayList<StepData>();
			for (String[] strings : stepList) {
				StepData stepData = new StepData();
				stepData.setCollectTime(format.parse(strings[0]));// time
				stepData.setSteps(Integer.parseInt(strings[1]));// step
				list.add(stepData);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			Log.d("Test","exception");
			return null;
		}
	}

	/**
	 * 获取所有计步数据
	 * 
	 * @return List<StepData>
	 */
	public List<StepData> getAllStepDatas() {
		try {
			List<StepData> list = ctx.queryForAll(StepData.class, int.class);
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 增加计步数据
	 * 
	 * @param startDate
	 * @param endDate
	 * @param maxSteps
	 */
	public void addStepData(Date startDate, Date endDate, int maxSteps) {
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);// 当前时间
		Calendar startCalendar = Calendar.getInstance();
		startCalendar.setTime(startDate);// 开始时间
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");// 定义日期格式
		try {
			ctx.beginTransaction();

			while (startCalendar.before(endCalendar)) {
				StepData stepData = new StepData();
				String dateString = format.format(startCalendar.getTime());
				stepData.setCollectTime(format.parse(dateString));
				stepData.setSteps(new Random().nextInt(maxSteps));
				startCalendar.add(startCalendar.DATE, 1);
				ctx.add(stepData, StepData.class, long.class);
			}
			if (startCalendar.equals(endCalendar)) {
				StepData stepData = new StepData();
				String dateString = format.format(startCalendar.getTime());
				stepData.setCollectTime(format.parse(dateString));
				stepData.setSteps(new Random().nextInt(maxSteps));
				ctx.add(stepData, StepData.class, long.class);
			}
			ctx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 如果出现异常，则回滚事务；否则，提交事务
			ctx.rollback();
		}
	}

	/**
	 * 删除所有计步数据
	 * @param stepDatas
	 */
	public void deleteAllStepDatas(List<StepData> stepDatas) {
		try {
			ctx.beginTransaction();
			for (StepData stepData : stepDatas) {
				ctx.delete(stepData, StepData.class,long.class);
			}
			ctx.commit();
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			ctx.rollback();
		}
	}

}
